<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @changeForm="changeForm"
      @handleQuery="getList(1)"
      @clear="clear"
    >
      <template #btns>
        <el-button v-if="meta.indexOf('ActivityDetailExcel') > -1" :loading="exportLoading" type="primary" @click="batchexport">导出</el-button>
      </template>
    </QueryForm>
    <div v-if="tjList && tjList.length > 0" class="total_wrap">
      <div class="first item">
        <div class="key">筛选</div>
        <div class="value">合计</div>
      </div>
      <div class="item_list">
        <div v-for="(item, i) in tjList" :key="i" class="item">
          <div class="key">{{ item.name }}</div>
          <div class="value">
            <!-- <span>¥</span> -->
            <span class="price">{{ item.value }}</span>
          </div>
        </div>
      </div>
      <!-- <el-tooltip effect="dark" placement="left">
        <div class="jesm">
          <img
            style="width: 15px; marginright: 2px"
            src="@/assets/icon/question.png"
            alt=""
          >金额说明
        </div>
        <div slot="content" v-html="smTip" />
      </el-tooltip> -->
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        label="用户名称"
        show-overflow-tooltip
        prop="name"
        min-width="120"
      />

      <el-table-column
        align="center"
        label="手机号"
        prop="phone"
        min-width="120"
      />
      <el-table-column
        align="center"
        label="活动名称"
        prop="activityName"
        width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="报名时间"
        prop="applyTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        label="支付金额"
        prop="price"
        min-width="100"
      />
      <el-table-column
        align="center"
        label="支付方式"
        min-width="100"
      >
        <template slot-scope="scope">
          <!-- 支付方式：01-会员卡,02-现金,03-微信,04-支付宝" -->
          {{
            scope.row.payMethod == '01'
              ? "会员卡"
              : scope.row.payMethod == '02'
                ? "现金"
                : scope.row.payMethod == '03'
                  ? "微信"
                  : scope.row.payMethod == '04'
                    ? "支付宝"
                    : ""
          }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="支付时间"
        prop="payTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        label="状态"
        fixed="right"
        min-width="100"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">待支付</span>
          <span v-if="scope.row.status == '1'">超时未支付</span>
          <span v-if="scope.row.status == '2'">取消支付</span>
          <span v-if="scope.row.status == '3'">报名成功</span>
          <span v-if="scope.row.status == '4'">退款中</span>
          <span v-if="scope.row.status == '5'">退款成功</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="filters.page"
        hide-on-single-page
        :total="totalCount"
        @current-change="handleCurrentChange"
      />
    </div> -->
    <Pagination
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>

import { listActivity, activityOrder, venueList, activityApplyManageExcel } from '@/api'
import Pagination from '@/components/paginations'
import { parseTime } from '@/utils'
import { queryFormConfig } from './config'
import dayjs from 'dayjs'
export default {
  name: 'ActivityDetail',
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      exportLoading: false,
      activitylist: [],
      list: [],
      meta: [],
      totalCount: 0,
      pagination: {
        page: 1,
        rows: 10
      },
      listLoading: false,
      statusList: [
        { value: '0', label: '篮球活动' },
        { value: '1', label: '足球活动' },
        { value: '2', label: '排球运动' }
      ],
      filters: {},
      tjList: [],
      smTip: '',
      venueList: []
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    let time = []
    const start = new Date()
    const end = new Date()
    start.setTime(end.getTime() - 3600 * 1000 * 24 * 29)
    time = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')]
    this.$set(this.filters, 'time', time)
    this.getVenueList()
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getVenueList() {
      venueList({
        param: { flag: '0' }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode !== '000000') return
          this.venueList = res.recordList
          // this.$set(this.filters, 'venueId', res.recordList[0].id)
          const arr = res.recordList
          this.queryFormConfig.formItems[0].options = arr
          // this.getActivtyList()
          this.getList(1)
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    changeForm(e) {
      if (e === 'venueId') {
        this.$set(this.filters, 'activityId', '')
        this.queryFormConfig.formItems[3].options = []
        this.activitylist = []
        if (this.filters.venueId) {
          this.getActivtyList()
        }
      }
    },
    getActivtyList() {
      sessionStorage.setItem('venueId', this.filters.venueId)
      listActivity({
        param: {
          venueId: sessionStorage.getItem('venueId')
        }
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.activitylist = res.recordList
          this.queryFormConfig.formItems[3].options = res.recordList
        })
        .catch(() => {})
    },
    getList(page) {
      this.listLoading = true
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      if (filters.time && filters.time.length > 0) {
        filters.startTime = filters.time[0] + ' 00:00:00'
        filters.endTime = filters.time[1] + ' 23:59:59'
      } else {
        filters.startTime = ''
        filters.endTime = ''
      }
      sessionStorage.setItem('venueId', this.filters.venueId)
      activityOrder({
        pagination,
        param: { isShop: '1', ...filters }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
            const tjList = res.data
            if (tjList) {
              let smTip = ''
              tjList.forEach(s => {
                smTip = smTip + s.tip + '<br />'
              })
              this.tjList = tjList
              this.smTip = smTip
            }
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    clear() {
      this.filters = {
        venueId: this.filters.venueId
      }
      this.getList(1)
    },
    batchexport() {
      sessionStorage.setItem('venueId', this.filters.venueId)
      const { filters } = this
      if (filters.time && filters.time.length > 0) {
        filters.startTime = filters.time[0] + ' 00:00:00'
        filters.endTime = filters.time[1] + ' 23:59:59'
      } else {
        filters.startTime = ''
        filters.endTime = ''
      }
      const venueId = sessionStorage.getItem('venueId')
      this.exportLoading = true
      activityApplyManageExcel({ param: { venueId, ...filters }}).then(res => {
        this.exportLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => { this.exportLoading = false })
    }
  }
}
</script>
